﻿using System;
using System.Net;
using System.Windows;
using GalaSoft.MvvmLight.Command;
using TemperatureWP.Model;
using TemperatureWP.Utilities;

namespace TemperatureWP.ViewModel
{
    public class TargetTemperatureViewModel : ExtendedViewModelBase
    {
        public TargetTemperatureViewModel()
        {
            // Initialize commands and looper
            OnLoadCommand = new RelayCommand(OnLoad);
            SaveTemperatureCommand = new RelayCommand(SaveTemperature);
            LoopingSelector = new IntLoopingDataSource { MinValue = -20, MaxValue = 40 };
        }

        public IntLoopingDataSource LoopingSelector { get; set; }

        private TemperatureModel _temperatureModel;
        public TemperatureModel TemperatureModel
        {
            get { return _temperatureModel; }
            set
            {
                if (value == null)
                    return;

                _temperatureModel = value;
                NotifyPropertyChanged(this, () => TemperatureModel);
            }
        }

        public RelayCommand OnLoadCommand { get; private set; }
        private void OnLoad()
        {
            // Get selected temeperature model
            TemperatureModel = ThisApp == null ? new TemperatureModel() : ThisApp.ActiveTemperature;

            // Set target temperature when view is shown
            LoopingSelector.SelectedItem = TemperatureModel.TargetTemperature;
        }

        public RelayCommand SaveTemperatureCommand { get; private set; }
        private void SaveTemperature()
        {
            if (TemperatureModel == null || LoopingSelector == null)
                return;

            // Get new target temperature
            TemperatureModel.TargetTemperature = Convert.ToDouble(LoopingSelector.SelectedItem);

            // Create uri
            var targetTemperature = TemperatureModel.TargetTemperature;
            var sensorId = TemperatureModel.Id;
            var sensorName = TemperatureModel.Name.Normalize();
            var sensorPlace = TemperatureModel.Place.Normalize();
            var setTargetTemperatureUri = string.Format("http://temperaturerest.cloudapp.net/Sensor/{0}/SetTargetTemperature/{1}/", sensorId, targetTemperature);
            var setNameUri = string.Format("http://temperaturerest.cloudapp.net/Sensor/{0}/SetName/{1}/", sensorId, sensorName);
            var setPlaceUri = string.Format("http://temperaturerest.cloudapp.net/Sensor/{0}/SetPlace/{1}/", sensorId, sensorPlace);

            SendData(setTargetTemperatureUri, GetResponse);
            SendData(setNameUri, GetResponse);
            SendData(setPlaceUri, GetResponse);

            Navigate(typeof(MainPage));
        }

        private void GetResponse(object sender, UploadStringCompletedEventArgs e)
        {
            if (e.Error != null)
                MessageBox.Show(string.Format("Error: {0}", e));
        }
    }

    public static class StringValidation
    {
        public static string Normalize(this string source)
        {
            var illegalCharaters = new[] { ' ', '/', '\\' };
            return string.Join("-", source.Split(illegalCharaters, StringSplitOptions.RemoveEmptyEntries));
        }
    }
}
